<script setup>
import {ref} from "vue";
import {http} from "@/api";
import {ElMessage} from "element-plus";
import useTable from "@/utils/useTable";
http.energyData.list().then(res => {
  const obj = {}
  res.data.forEach(item => {
    obj[item.code] = item.value
  })
  for (const formData1Key in formData1.value) {
    if (formData1.value.hasOwnProperty(formData1Key)) {
      formData1.value[formData1Key] = obj[formData1Key]
    }
  }
  for (const formData2Key in formData2.value) {
    if (formData2.value.hasOwnProperty(formData2Key)) {
      formData2.value[formData2Key] = obj[formData2Key]
    }
  }
  for (const formData3Key in formData3.value) {
    if (formData3.value.hasOwnProperty(formData3Key)) {
      formData3.value[formData3Key] = obj[formData3Key]
    }
  }
})
/**
 * 第一个表单
 * @type {Ref<UnwrapRef<null>>}
 */
const formRef1 = ref(null)
const formData1 = ref({
  // 全场额定功率
  rated_power: '',
  // 全场额定容量
  rated_capacity: '',
  // 站用变用电量
  station_electricity: '',
  // 35Kv电缆温度
  cable_temperature: '',
})


const rules1 = ref({
  rated_power: [
    {required: true, message: '请输入全场额定功率', trigger: 'blur'},
  ],
  rated_capacity: [
    {required: true, message: '请输入全场额定容量', trigger: 'blur'},
  ],
  station_electricity: [
    {required: true, message: '请输入站用变用电量', trigger: 'blur'},
  ],
  cable_temperature: [
    {required: true, message: '请输入35Kv电缆温度', trigger: 'blur'},
  ],
})
const submit1 = () => {
  formRef1.value.validate((valid) => {
    if (valid) {
      console.log('submit!')
      http.energyData.update(formData1.value).then(res => {
        console.log(res)
        ElMessage.success(res.msg)
      })
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset1 = () => {
  formRef1.value.resetFields()
}
/**
 * 第二个表单
 * @type {Ref<UnwrapRef<null>>}
 */
const formRef2 = ref(null)
const formData2 = ref({
  active_power: '',
  inactive_power: '',
})

const rules2 = ref({})
const submit2 = () => {
  formRef2.value.validate((valid) => {
    if (valid) {
      http.energyData.update(formData2.value).then(res => {
        console.log(res)
        ElMessage.success(res.msg)
      })
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset2 = () => {
  formRef2.value.resetFields()
}
/**
 * 第三个表单
 */
const formRef3 = ref(null)
const formData3 = ref({
  // 电池剩余充电量
  battery_remaining_charge: '',
  // 电池剩余充电时间
  battery_remaining_time: '',
  // 当前最大可充功率
  max_charge_power: '',
  // 当前最大可放功率
  max_discharge_power: '',
  // 当前最大可充电量
  max_charge_capacity: '',
  // 当前最大可放电量
  max_discharge_capacity: '',
  // 额定功率充电时间
  rated_power_charge_time: '',
  // 额定功率放电时间
  rated_power_discharge_time: '',
  // 今日充电量
  today_charge_capacity: '',
  // 今日放电量
  today_discharge_capacity: '',
  // 累计充电量
  total_charge_capacity: '',
  // 累计放电量
  total_discharge_capacity: '',
})


const rules3 = ref({})
const submit3 = () => {
  formRef3.value.validate((valid) => {
    if (valid) {
      http.energyData.update(formData3.value).then(res => {
        console.log(res)
        ElMessage.success(res.msg)
      })
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset3 = () => {
  formRef3.value.resetFields()
}

/**
 * 电量曲线
 */
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openDelete
} = useTable({
  requestFn: http.energyTimeData,
})
const editDateData = (row) => {
  formData4.value = row
  show.value = true
}
// 弹窗
const show = ref(false)
const addDateData = () => {
  formData4.value = {
    id: undefined,
    name: '',
    value: '',
  }
  show.value = true
}

const formRef4 = ref(null)
const formData4 = ref({
  id: undefined,
  name: '',
  value: '',
})
const rules4 = ref({
  name: [
    {required: true, message: '请输入时间', trigger: 'blur'},
  ],
  value: [
    {required: true, message: '请输入值', trigger: 'blur'},
  ],
})
const submit4 = () => {
  formRef4.value.validate((valid) => {
    if (valid) {
      console.log('submit!')
      if (formData4.value.id) {
        http.energyTimeData.update(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      } else {
        http.energyTimeData.create(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset4 = () => {
  show.value = false
}

const downloadTemplate = () => {

}
const uploadFile = (file) => {
  // 上传头像
  http.energyTimeData.upload(file.file).then(res => {
    search()
  })
}
</script>

<template>
<div class="data-container">
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <span>基本信息</span>
      </div>
    </template>
    <el-form ref="formRef1" :model="formData1" :rules="rules1" label-position="top">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="全场额定功率" prop="rated_power">
            <el-input v-model="formData1.rated_power"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="全场额定功率" prop="rated_capacity">
            <el-input v-model="formData1.rated_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="站用变用电量" prop="station_electricity">
            <el-input v-model="formData1.station_electricity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="35Kv电缆温度" prop="cable_temperature">
            <el-input v-model="formData1.cable_temperature"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item >
        <div style="text-align: center;width: 100%">

          <el-button type="primary" @click="submit1">提交</el-button>
        </div>
      </el-form-item>
    </el-form>

  </el-card>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <span>PCS实时功率</span>
      </div>
    </template>
    <el-form ref="formRef2" :model="formData2" :rules="rules2" label-position="top">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="有功功率" prop="active_power">
            <el-input v-model="formData2.active_power"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="无功功率" prop="inactive_power">
            <el-input v-model="formData2.inactive_power"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item >
        <div style="text-align: center;width: 100%">
          <el-button @click="reset2">重置</el-button>
          <el-button type="primary" @click="submit2">提交</el-button>
        </div>
      </el-form-item>
    </el-form>

  </el-card>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <span>电池信息</span>
      </div>
    </template>
    <el-form ref="formRef3" :model="formData3" :rules="rules3" label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="电池剩余充电量" prop="battery_remaining_charge">
            <el-input v-model="formData3.battery_remaining_charge" min="0" max="100" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池剩余充电时间" prop="battery_remaining_time">
            <el-input v-model="formData3.battery_remaining_time"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="当前最大可充功率" prop="max_charge_power">
            <el-input v-model="formData3.max_charge_power"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="当前最大可放功率" prop="max_discharge_power">
            <el-input v-model="formData3.max_discharge_power"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="当前可充电量" prop="max_charge_capacity">
            <el-input v-model="formData3.max_charge_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="当前可放电量" prop="max_discharge_capacity">
            <el-input v-model="formData3.max_discharge_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="额定功率充电时间" prop="rated_power_charge_time">
            <el-input v-model="formData3.rated_power_charge_time"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="额定功率放电时间" prop="rated_power_discharge_time">
            <el-input v-model="formData3.rated_power_discharge_time"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="今日充电量" prop="today_charge_capacity">
            <el-input v-model="formData3.today_charge_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="今日放电量" prop="today_discharge_capacity">
            <el-input v-model="formData3.today_discharge_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="累计充电量" prop="total_charge_capacity">
            <el-input v-model="formData3.total_charge_capacity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="累计放电量" prop="total_discharge_capacity">
            <el-input v-model="formData3.total_discharge_capacity"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item >
        <div style="text-align: center;width: 100%">
          <el-button @click="reset3">重置</el-button>
          <el-button type="primary" @click="submit3">提交</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <span>电量信息</span>
      </div>
    </template>
    <div style="text-align: right;margin-bottom: 10px;display: flex;align-items: center;justify-content: flex-end">
      <el-button type="primary" @click="search">刷新</el-button>
      <el-button type="primary" @click="addDateData">添加</el-button>
      <div>
        <el-upload
            action="#"
            accept=".xlsx"
            :http-request="uploadFile"
            :show-file-list="false"
            style="display: flex;align-items: center" >
          <el-button type="primary">
            上传
          </el-button>
        </el-upload>
      </div>
      <el-button type="primary" @click="downloadTemplate('')">下载模板</el-button>
    </div>
    <div>
      <el-table height="500px" :data="dataSource">
        <el-table-column prop="name" label="时间"></el-table-column>
        <el-table-column prop="value" label="值"></el-table-column>
        <el-table-column>
          <template #default="{row}">
            <el-button type="text" @click="editDateData(row)">编辑</el-button>
            <el-button type="text" @click="openDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </el-card>
  <el-dialog v-model="show" title="新增/编辑" width="500px">
    <el-form ref="formRef4" :model="formData4" :rules="rules4" label-width="80px">
      <el-form-item label="时间" prop="name">
        <el-date-picker v-model="formData4.name" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="值" prop="value">
        <el-input v-model="formData4.value" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset4">取消</el-button>
        <el-button type="primary" @click="submit4">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<style scoped lang="scss">
.data-container {
  width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  .el-card {
    margin-bottom: 12px;
  }
}
</style>